package com.tojabo.hash;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class HashTableApp {

	
	public static void main(String[] args) throws IOException {
		
		DataItem aDataItem;
		int aKey,size,n,keysPerCell;
		
		System.out.println("Enter size of hash table:");
		size=getInt();
		System.out.println("Enter initial number of items:");
		n=getInt();
		keysPerCell = 10;
		
		HashTable theHashTable = new HashTable(size);
		
		for(int j=0;j<n;j++){
			aKey = (int) (Math.random()*keysPerCell*size);
			aDataItem = new DataItem(aKey);
			theHashTable.insert(aDataItem);
		}
		
		while(true){
			System.out.print("Enter first letter of ");
			System.out.print("show, insert, delete, or find: ");
			char choice = getChar();
			switch(choice){
			case 's':
				theHashTable.displayTable();
				break;
			case 'i':
				System.out.print("Enter key value to insert:");
				aKey = getInt();
				aDataItem = new DataItem(aKey);
				theHashTable.insert(aDataItem);
				break;
			case 'd':
				System.out.print("Enter key value to delete:");
				aKey = getInt();
				theHashTable.delete(aKey);
				break;
			case 'f':
				System.out.print("Enter key value to delete:");
				aKey = getInt();
				aDataItem=theHashTable.find(aKey);
				if(aDataItem!=null){
					System.out.print("Found :"+aKey);
				}else{
					System.out.print("Could notFound :"+aKey);
				}
				break;
			default:
				System.out.print("Invalid entry\n");
			}//end switch
		}//end while
		
	}//end main

	public static String getString() throws IOException{
		InputStreamReader inStrem = new InputStreamReader(System.in);
		BufferedReader read = new BufferedReader(inStrem);
		String s = read.readLine();
		return s;
	}
	
	private static char getChar() throws IOException {
		String s = getString();
		
		return s.charAt(0);
	}

	private static int getInt() throws IOException {
		String s = getString();
		return Integer.parseInt(s);
	}

}
